package com.mai4j.maiadmin.service.impl;

import com.mai4j.maiadmin.generated.tables.records.SqlHistoryRecord;
import com.mai4j.maiadmin.misc.DbUtils;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.dto.SqlHistoryDTO;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.service.QueryService;
import com.mai4j.maiadmin.service.SqlHistoryService;
import com.mai4j.maiadmin.support.IdProvider;
import com.mai4j.maiadmin.support.JooqProvider;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.SelectWhereStep;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.mai4j.maiadmin.generated.Tables.SQL_HISTORY;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@Service
public class SqlHistoryServiceImpl implements SqlHistoryService {

    private final QueryService queryService;

    public SqlHistoryServiceImpl(QueryService queryService) {
        this.queryService = queryService;
    }

    @Transactional
    @Override
    public PageDTO<SqlHistoryDTO> paginate(Datatable datatable) {
        DSLContext dsl = JooqProvider.dsl();
        SelectWhereStep<SqlHistoryRecord> query = dsl.selectFrom(SQL_HISTORY);
        List<Condition> conditions = new ArrayList<>();
        conditions.add(DbUtils.toJooqConditions(datatable.getConditions()));
        query.where(conditions);
        int count = queryService.fetchCount(query).count;
        List<SqlHistoryDTO> languageDTOS = count == 0 ? Collections.emptyList() : query
                .orderBy(SQL_HISTORY.SQL_HISTORY_ID.desc())
                .limit(datatable.getStart(), datatable.getLength())
                .fetchInto(SqlHistoryDTO.class);
        return new PageDTO<>(languageDTOS, DbUtils.getPage(datatable), datatable.getLength(), DbUtils.getTotalPage(count, datatable), count);
    }

    @Override
    public void save(SqlHistoryDTO sqlHistoryDTO) {
        JooqProvider.dsl().insertInto(SQL_HISTORY)
                .columns(
                        SQL_HISTORY.SQL_HISTORY_ID,
                        SQL_HISTORY.APP_NAME,
                        SQL_HISTORY.VISITOR,
                        SQL_HISTORY.IP,
                        SQL_HISTORY.FORWARDED_IP,
                        SQL_HISTORY.USER_AGENT,
                        SQL_HISTORY.CONNECTION,
                        SQL_HISTORY.ELAPSED,
                        SQL_HISTORY.STATEMENT,
                        SQL_HISTORY.TABLE_NAME,
                        SQL_HISTORY.SQL_TEXT,
                        SQL_HISTORY.SQL_DATE,
                        SQL_HISTORY.DATE_ADDED)
                .values(
                        IdProvider.next(),
                        sqlHistoryDTO.getAppName(),
                        sqlHistoryDTO.getVisitor(),
                        sqlHistoryDTO.getIp(),
                        sqlHistoryDTO.getForwardedIp(),
                        sqlHistoryDTO.getUserAgent(),
                        sqlHistoryDTO.getConnection(),
                        sqlHistoryDTO.getElapsed(),
                        sqlHistoryDTO.getStatement(),
                        sqlHistoryDTO.getTableName(),
                        sqlHistoryDTO.getSqlText(),
                        sqlHistoryDTO.getSqlDate(),
                        LocalDateTime.now())
                .execute();
    }
}
